package jp.co.systemi.asp.workflow;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.logging.Logger;

import org.apache.wicket.Request;
import org.apache.wicket.authentication.AuthenticatedWebSession;
import org.apache.wicket.authorization.strategies.role.Roles;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * セッション管理したい値を制御
 */
public class WorkflowSession extends AuthenticatedWebSession implements
        Serializable {
    private static final long serialVersionUID = 1L;

    static Logger log = Logger.getLogger(WorkflowSession.class.getName());

    /** アクティブメニュー項目はリンクなし、黒字で表示する */
    private String activeMenu = "";

    public String getActiveMenu() {
        return activeMenu;
    }

    public void setActiveMenu(String activeMenu) {
        this.activeMenu = activeMenu;
    }

    private boolean verified = false;

    public boolean isVerified() {
        return verified;
    }

    public void setVerified(boolean verified) {
        this.verified = verified;
    }

    public boolean isLogin = false;

    protected WorkflowSession(Request request) {
        super(request);
        // InjectorHolder.getInjector().inject(this);
    }

    public void clearDownloadKey() {
        dirty();
    }

    public void clearAuth() {
        dirty();
    }

    /** 認証ユーザのアカウントID ※現行の流用 */
    private BigInteger accountId = BigInteger.ZERO;

    public BigInteger getAccountId() {
        return accountId;
    }

    public void setAccountId(BigInteger accountId) {
        this.accountId = accountId;
        dirty();
    }

    public void login() {
        this.isLogin = true;
        dirty();
    }

    public void logout() {
        this.isLogin = false;
        this.accountId = BigInteger.ZERO;
        this.accountMonth = "";
        this.accountYear = "";
        this.keepAccountYearMonth = false;
        this.logMonth = "";
        this.logYear = "";
        this.keepLogYearMonth = false;
        dirty();
    }

    private String accountYear = "";
    private String accountMonth = "";
    boolean keepAccountYearMonth = false;

    public void setAccountYearMonth(String year, String month) {
        this.accountYear = year;
        this.accountMonth = month;
        keepAccountYearMonth = true;
        dirty();
    }

    public String getAccountMonth() {
        return this.accountMonth;
    }

    public String getAccountYear() {
        return this.accountYear;
    }

    public void clearAccountYearMonth() {
        if (this.keepAccountYearMonth) {
            keepAccountYearMonth = false;
        } else {
            this.accountMonth = "";
            this.accountYear = "";
        }
        dirty();
    }

    private String logYear = "";
    private String logMonth = "";
    boolean keepLogYearMonth = false;

    public void setLogYearMonth(String year, String month) {
        this.logYear = year;
        this.logMonth = month;
        keepLogYearMonth = true;
        dirty();
    }

    public String getLogMonth() {
        return this.logMonth;
    }

    public String getLogYear() {
        return this.logYear;
    }

    public void clearLogYearMonth() {
        if (this.keepLogYearMonth) {
            keepLogYearMonth = false;
        } else {
            this.logMonth = "";
            this.logYear = "";
        }
        dirty();
    }

    /**
     * ログイン者は管理権限を持つ管理者ですか
     *
     * @return boolean true：管理権限を持つ管理者、false：参照権限のみの管理者
     */
    public boolean isManager() {
        return true;
    }

    @Override
    public boolean authenticate(String username, String password) {
        UserService userService = UserServiceFactory.getUserService();
        com.google.appengine.api.users.User usr = userService.getCurrentUser();
        String loginId = usr.getEmail();
        log.info("loginId = " + loginId);
        return userService.isUserLoggedIn();
    }

    @Override
    public Roles getRoles() {
        UserService userService = UserServiceFactory.getUserService();
        com.google.appengine.api.users.User usr = userService.getCurrentUser();
        String loginId = usr.getEmail();
        log.info("loginId = " + loginId + " isUserAdmin: " + userService.isUserAdmin());
        if (userService.isUserAdmin()) {
            return new Roles(new String[] { Roles.ADMIN, Roles.USER });
        }

        return new Roles(new String[] { Roles.USER });
    }

}
